<script setup>
import { ref } from "vue";
import { useCounterStore } from "@/stores/counter.js";
import { getIndexList } from "@/http/api.js";
import { reactive } from "vue";
const store = useCounterStore();
const search = ref("");
const data = reactive({
  list: [],
});
const serach = async () => {
  if (search.value == "") {
    return (data.list = []);
  }
  const res = await getIndexList(store.Arr[0].id, search.value);
  data.list = res;
  // console.log(res);
  store.getHistory(search.value);
  search.value = "";
};

const lis = (val) => {
  search.value = val;
};
</script>
<template>
  <div class="search">
    <header>
      <p><van-icon name="arrow-left" @click="$router.back()" /></p>
      <p>
        <input
          type="text"
          placeholder="请输入内容"
          v-model="search"
          @keyup.enter="serach"
        />
      </p>
      <p>取消</p>
    </header>
    <!-- 列表 -->
    <ul class="list" v-if="data.list.length > 0">
      <li
        v-for="(item, index) in data.list"
        :key="item.id"
        @click="$router.push(`/goods/${item.id}`)"
      >
        <span class="sp">{{ item.course_classify_title }}</span>
        <p class="p1">{{ item.title }}</p>
        <div class="div1">
          <p
            v-for="(item1, index1) in item.teacher"
            :key="index1"
            class="teacher"
          >
            <img :src="item1.avatar" alt="" />
            <span>{{ item1.teacher_name }}</span>
          </p>
        </div>
        <div class="ren">
          <p>{{ item.sales_num }}人已报名</p>
          <p style="color: red">￥{{ item.price / 10000 }}</p>
        </div>
      </li>
    </ul>
    <p v-if="!data.list.length"><van-empty description="描述文字" /></p>
    <!-- 历史搜索 -->
    <div class="box" v-if="!data.list.length">
      <h3>历史搜索</h3>
      <van-icon name="circle" @click="store.delAll" />
    </div>
    <ul class="ul1" v-if="!data.list.length">
      <li
        v-for="(item, index) in store.history"
        :key="index"
        @click="lis(item)"
      >
        {{ item }}
      </li>
    </ul>
  </div>
</template>
<style lang="scss" scoped>
.search {
  height: 100%;
  // padding: 0 10px;
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 88px;
    line-height: 88px;
    font-size: 18px;
    color: #ccc;
    padding: 10px 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.205);
    input {
      width: 7.4667rem;
      height: 60px;
      border: none;
      background: #e4e7ed;
      border-radius: 40px;
      padding-left: 30px;
      font-size: 16px;
      margin-top: 10px;
    }
    .van-icon {
      font-size: 25px;
      margin-top: 40px;
    }
  }
  .list {
    padding: 0 20px;
    margin-top: 150px;
    margin-bottom: 100px;

    li {
      font-size: 32px;
      margin: 30px 0;
      background: #fff;
      border-radius: 20px;
      box-shadow: 0 0.04rem 0.09333rem 0 hsl(0deg 0% 81% / 35%);
      .sp {
        width: 120px;
        font-size: 25px;
        padding: 10px 10px;
        background: orangered;
        color: #fff;
      }
      .p1 {
        margin-left: 20px;
      }
      .div1 {
        display: flex;
        align-items: center;
        margin: 40px 40px;
        .teacher {
          margin: 20px 20px;
        }
        img {
          width: 50px;
          height: 50px;
          vertical-align: middle;
          border-radius: 50%;
        }
        span {
          margin-left: 10px;
          font-size: 20px;
        }
      }
      .ren {
        display: flex;
        justify-content: space-between;
        padding: 0 40px;
      }
    }
  }
  .box {
    display: flex;
    padding: 0 40px;
    justify-content: space-between;
    align-items: center;
    h3 {
      font-size: 20px;
    }
    .van-icon {
      color: #f00;
      font-size: 20px;
    }
  }
  .ul1 {
    display: flex;
    padding: 0 60px;
    font-size: 12px;
    flex-wrap: wrap;
    margin-top: 20px;
    li {
      width: 20%;
      padding: 10px 5px;
      border-radius: 30px;
      background: #f7f7f7;
      margin: 20px 20px;
      text-align: center;
    }
  }
}
</style>
